<?php
require_once("../resources/commons.php");
require_once("../data/Activity.php");
require_once("../data/Activitytype.php");
require_once("../data/User.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/text.php");
require_once("HTML/QuickForm/textarea.php");
require_once("HTML/QuickForm/select.php");
require_once("HTML/QuickForm/checkbox.php");
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/hidden.php");
require_once("admin_commons.php");

/**
 * Activity creation form.
 * This form is used to create a new activity.
 */
class UpdateActivityPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
        
		$l_idInput=new HTML_QuickForm_hidden("id");
		$this->addElement($l_idInput);
        
		$l_titleInput=new HTML_QuickForm_text("title");
		$l_titleInput->setSize(60);
		$l_titleInput->setLabel("Title :");
		$this->addElement($l_titleInput);
		
		$l_descriptionInput=new HTML_QuickForm_textarea("description");
		$l_descriptionInput->setCols(60);
		$l_descriptionInput->setRows(5);
		$l_descriptionInput->setLabel("Description :");
		$this->addElement($l_descriptionInput);
		
		$l_isArchivedCheckbox=new HTML_QuickForm_checkbox("isArchived");
		$l_isArchivedCheckbox->setLabel("Is archived :");
		$this->addElement($l_isArchivedCheckbox);
		
		$l_activityTypeSelect=new HTML_QuickForm_select("activityType");
		$l_activityTypeSelect->setLabel("Type of activity: ");
		$l_records=ActivityType::getAll();
		foreach($l_records as $l_activityType){
			$l_activityTypeSelect->addOption($l_activityType->getLabel(),base64_encode($l_activityType->getId()));
		}
		$this->addElement($l_activityTypeSelect);
		
		$l_responsibleSelect=new HTML_QuickForm_select("responsible");
		$l_responsibleSelect->setLabel("In charge: ");
		$l_records=User::getAll();
		foreach($l_records as $l_user){
			$l_responsibleSelect->addOption($l_user->getFullName(),base64_encode($l_user->getId()));
		}
		$this->addElement($l_responsibleSelect);
		
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		//The validation rules (either client and server)
		$this->addRule("title","An activity must have a title","required",null,"client");
		$this->addRule("title","An activity must have a title","required",null,"server");
		$this->addRule("activityType","Activity type must not be empty","required",null,"server");
		$this->addRule("responsible","An activity must be associated with a person in charge","required",null,"server");
		
		$this->applyFilter("title","trim");
		$this->applyFilter("description","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){
    	checkAdminRights();
    	
    	displayTop("Activity edition","../resources/htmledit.php",null);
    	echo "<p>Create or update an activity</p>";
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	checkAdminRights();
    	
    	$l_id=$page->exportValue("id");
    	$l_title=$page->exportValue("title");
    	$l_description=$page->exportValue("description");
    	$l_isArchived=$page->exportValue("isArchived");
    	$l_isArchived=!is_null($l_isArchived);
    	$l_activityType=$page->exportValue("activityType");
    	$l_responsible=$page->exportValue("responsible");
    	
    	$l_activity=new Activity();
        if(trim($l_id)!=""){
    		$l_activity->setId($l_id);
    	}
    	$l_activity->setTitle($l_title);
    	$l_activity->setDescription($l_description);
    	$l_activity->setIsArchived($l_isArchived);
    	$l_activity->setActivityType_id(base64_decode($l_activityType));
    	$l_activity->setResponsibleUser_id(base64_decode($l_responsible));
    	$l_activity->save();
    	
    	HTTP::redirect("manage_activities.php");
    }
}

$controller =new HTML_QuickForm_Controller('ActivityCreationProcess', false);
$controller->addPage(new UpdateActivityPage('updateActivity'));
$controller->container(true);

if(isset($_REQUEST["updateId"])){
	//An id is provided as a parameter, this is an update
	$l_activityId=base64_decode($_REQUEST["updateId"]);
	$l_activity=Activity::getFromPrimaryKey($l_activityId);
	$controller->setDefaults(array(
		"id" => $l_activity->getId(),
		"title" => $l_activity->getTitle(),
		"description" => $l_activity->getDescription(),
		"isArchived" => $l_activity->getIsArchived(),
		"activityType" => base64_encode($l_activity->getActivityType_id()),
		"responsible" => base64_encode($l_activity->getResponsibleUser_id())
	));
}

$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();
?>